package com.zhazha.earlybird.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zhazha.earlybird.beans.CampusRecruitInfo;

public class CollectCampusRecruitDAO {

	private SQLiteDatabase mDatabase;
	
	private static final String COLLECT_CAMPUS_RECRUIT_TABLE = "collect_campus_recruit";
	
	private static final String CAMPUS_RECRUIT_ID = "id"; 
	private static final String CAMPUS_RECRUIT_TITLE = "title";
	private static final String CAMPUS_RECRUIT_COMPANY = "company";
	private static final String CAMPUS_RECRUIT_POSITION_ID = "position_id";
	private static final String CAMPUS_RECRUIT_CITY_ID = "city_id";
	private static final String CAMPUS_RECRUIT_HIT_COUNT = "hit_count";
	
	public CollectCampusRecruitDAO(SQLiteDatabase datebase) {
		this.mDatabase = datebase;
	}
	
	public CampusRecruitInfo queryById(int id) {
		CampusRecruitInfo info = null;
		
		try{
			String selection = "id=?";
			String[] selectionArgs = {String.valueOf(id)};
			Cursor c = this.mDatabase.query(COLLECT_CAMPUS_RECRUIT_TABLE, null, selection, selectionArgs, null, null, null);
			if(c.moveToFirst()) {
				
				info = new CampusRecruitInfo();
				
				info.setId(c.getInt(c.getColumnIndex(CAMPUS_RECRUIT_ID)));
				info.setTitle(c.getString(c.getColumnIndex(CAMPUS_RECRUIT_TITLE)));
				info.setCompany(c.getString(c.getColumnIndex(CAMPUS_RECRUIT_COMPANY)));
				info.setHitCount(c.getInt(c.getColumnIndex(CAMPUS_RECRUIT_HIT_COUNT)));
				info.setCityId(c.getInt(c.getColumnIndex(CAMPUS_RECRUIT_CITY_ID)));
				info.setPositionId(c.getInt(c.getColumnIndex(CAMPUS_RECRUIT_POSITION_ID)));
				
				
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return info;
		
	}
	
	public CampusRecruitInfo[] queryAll() {
		CampusRecruitInfo[] info = null;
		
		try {
			
			 Cursor c = this.mDatabase.query(COLLECT_CAMPUS_RECRUIT_TABLE, null, null, null, null, null, "id asc");
			
			if(c.moveToFirst()) {
				info = new CampusRecruitInfo[c.getCount()];
				for (int i = 0; i < c.getCount(); i++) {
					
					info[i] = new CampusRecruitInfo();
					info[i].setId(c.getInt(c.getColumnIndex(CAMPUS_RECRUIT_ID)));
					info[i].setTitle(c.getString(c.getColumnIndex(CAMPUS_RECRUIT_TITLE)));
					info[i].setCompany(c.getString(c.getColumnIndex(CAMPUS_RECRUIT_COMPANY)));
					info[i].setHitCount(c.getInt(c.getColumnIndex(CAMPUS_RECRUIT_HIT_COUNT)));
					info[i].setCityId(c.getInt(c.getColumnIndex(CAMPUS_RECRUIT_CITY_ID)));
					info[i].setPositionId(c.getInt(c.getColumnIndex(CAMPUS_RECRUIT_POSITION_ID)));

					c.moveToNext();
				}
				return info;
				
			}
			
			return info;
		}
		catch (Exception e) {
			e.printStackTrace();
			return info;
		}
	}
	
	public void deleteAll() {
		
		try{
			this.mDatabase.delete(COLLECT_CAMPUS_RECRUIT_TABLE, null, null);
		}
		catch( Exception e) {
			e.printStackTrace();
		}
	}
	
	public void insert(CampusRecruitInfo info) {
		
		try{
				ContentValues cv = new ContentValues();
				cv.put(CAMPUS_RECRUIT_ID, info.getId());
				cv.put(CAMPUS_RECRUIT_TITLE, info.getTitle());
				cv.put(CAMPUS_RECRUIT_COMPANY, info.getCompany());
				cv.put(CAMPUS_RECRUIT_HIT_COUNT, info.getHitCount());
				cv.put(CAMPUS_RECRUIT_CITY_ID, info.getCityId());
				cv.put(CAMPUS_RECRUIT_POSITION_ID, info.getPositionId());

				this.mDatabase.insert(COLLECT_CAMPUS_RECRUIT_TABLE, null, cv);
			
		}
		catch( Exception e){
			e.printStackTrace();
			
		}
	}
	
	public void delete(int id) {
		
		try {
			
			String whereClause = "id=?";
			String[] whereArgs = {String.valueOf(id)};
			this.mDatabase.delete(COLLECT_CAMPUS_RECRUIT_TABLE, whereClause, whereArgs);
		}
		catch ( Exception e) {
			e.printStackTrace();
		}
	}
}
